<?php

namespace App\Http\Controllers;

use App\FormModel;
use function Couchbase\defaultDecoder;
use Illuminate\Http\Request;

class AbduController extends Controller
{
    public function form(Request $request)
    {
        $id = $request->input('id');
        $uqur = FormModel::where('id', $id)->first();
        $data = ['uqur' => $uqur];
        return view("form")->with($data);
    }

    public function add(Request $request){
        $input = $request->input();
        $id=$input['id'];
        unset($input['_token']);
        if ($id==0){
            $img = $request->file("pic");
            $imgName = $img->getClientOriginalName();
            $OK = $img->move(public_path("upload"), $imgName);
            if ($OK) {
                $input['img'] = "upload/" . $imgName;
                $yes = FormModel::create($input);
                if ($yes) {
                    return "ok!";
                } else {
                    return "NO!";
                }
            }
        }else{
            $xx=FormModel::where('id',$id)->first();
            $xx->update($input);
            echo $xx?'update_ok':'update_no';
        }

    }

    public function table(){
        $ab=FormModel::get();
        return view('table',compact("ab"));
    }

    public function delete(Request $request){
        $id=$request->input(['id']);
        $ok=FormModel::where('id',$id)->delete();
        echo $ok?'delete_ok':'delete_no';
    }

}
